package com.cheyunkeji.er.app;

import android.content.Context;
import android.util.Base64;

import com.cheyunkeji.er.utils.EncryptUtils;

/**
 * Created by Michael on 2017/4/20 14:33.
 */

public class UserInfoManager extends MyPreferencesManager {

//  用户账号可以是电话号码也可以是邮箱
    private static final String KEY_ACCOUNT = "account";
    private static final String KEY_MOBILE = "phone";
    private static final String KEY_PWD = "password";
    private static final String M_ID = "m_id";
    private static final String AUTH_KEY = "auth_key";
    private static final String REAL_NAME = "real_name";
    private static final String KEY_QQ = "qq";
    private static final String KEY_SEX = "sex";

//    是否第一次开启APP
    private static final String IS_FIRST_START_APP = "first_start_app" ;

//  当前选中通道的ckey
    private static final String C_KEY  = "current_channel_ckey";

    private static final String FILE_NAME = "user";
    private static UserInfoManager sInstance = null;

    public UserInfoManager(Context context) {
        super(context, FILE_NAME);
    }

    public static UserInfoManager getInstance(Context context) {
        synchronized (UserInfoManager.class) {
            if (sInstance == null) {
                sInstance = new UserInfoManager(context);
            }
        }
        return sInstance;
    }


    /**
     * 保存用户的账号、密码（Base64 保存）
     *
     * @param account  明文账号
     * @param password 明文密码
     */
    public UserInfoManager saveAccountAndPsd(String account, String password) {
        putString(KEY_ACCOUNT, Base64.encodeToString(account.getBytes(), Base64.DEFAULT));
        putString(KEY_PWD, password) ;
        return this;
    }

    /**
     * 保存用户账号(Base64)
     *
     * @param account 明文账号
     */
    public UserInfoManager saveAccount(String account) {
        putString(KEY_ACCOUNT, Base64.encodeToString(account.getBytes(), Base64.DEFAULT));
        return this;
    }

    /**
     * 保存用户密码(MD5)
     *
     * @param password 明文密码
     */
    public UserInfoManager savePwd(String password) {
        putString(KEY_PWD, EncryptUtils.encryptMD5ToString(password.getBytes()));
        return this;
    }

    /**
     * 读取用户账号(明文)
     */
    public String getAccount() {
        return new String(Base64.decode(getString(KEY_ACCOUNT, ""), Base64.DEFAULT));
    }

    /**
     * 保存当前通道的ckey
     * @param ckey
     */
    public void saveCKey(String ckey){
        putString(C_KEY,Base64.encodeToString(ckey.getBytes(), Base64.DEFAULT));
    }

    /**
     * 读取当前通道的ckey
     * @return
     */
    public String getCKey(){
        return new String(Base64.decode(getString(C_KEY, ""), Base64.DEFAULT));
    }


    /**
     * 读取用户密码，明文的密码
     */
    public String getPwd() {
        return new String(getString(KEY_PWD,""));
    }

    /**
     * 保存当前用户的MID
     */
    public UserInfoManager saveMid(String mid) {
        putString(M_ID, mid);
        return this;
    }

    public String getMId(){
        return getString(M_ID,"");
    }


    /**
     * 保存当前用户的电话号码
     */
    public UserInfoManager saveMobile(String phoneNumber) {
        putString(KEY_MOBILE, phoneNumber);
        return this;
    }

    public String getMobile(){
        return getString(KEY_MOBILE,"");
    }


    public UserInfoManager saveFirtStartTag (boolean isFirstStratApp){
        putBool(IS_FIRST_START_APP,isFirstStratApp);
        return this ;
    }

    public boolean isFirstStartApp(){
        return getBool(IS_FIRST_START_APP,true);
    }

    /**
     * 保存当前用户的authkey
     */
    public UserInfoManager saveAuthKey(String authKey) {
        putString(AUTH_KEY, authKey);
        return this;
    }

    public String getAuthKey(){
        return getString(AUTH_KEY,"");
    }

    /**
     * 保存当前用户的性别
     */
    public UserInfoManager saveGender(String gender) {
        putString(KEY_SEX, gender);
        return this;
    }

    public String getGender(){
        return getString(KEY_SEX,"");
    }

    /**
     * 保存当前用户的真实姓名
     */
    public UserInfoManager saveRealName(String realName) {
        putString(REAL_NAME, realName);
        return this;
    }

    public String getRealName(){
        return getString(REAL_NAME,"");
    }

    /**
     * 保存当前用户的qq
     */
    public UserInfoManager saveQQ(String realName) {
        putString(KEY_QQ, realName);
        return this;
    }

    public String getQq(){
        return getString(KEY_QQ,"");
    }

    /**
     * 保存当前模块
     * @param currentModuleType
     */
    public UserInfoManager saveCurrentModuleType(int currentModuleType) {
        putInt("currentModuleType", currentModuleType);
        return this;
    }
    /**
     * 得到当前模块
     * @param
     */
    public int getCurrentModuleType() {
        return getInt("currentModuleType", 0);
    }


    /**
     * 保存当前通道key
     * @param y7e_ckey
     */
    public UserInfoManager saveY7e_ckey(String y7e_ckey) {
        putString("y7e_ckey", y7e_ckey);
        return this;
    }

    /**
     * 得到当前通达可以
     * @return
     */
    public String getY7e_ckey() {
        return getString("y7e_ckey","");
    }
}
